<?php
/**
 * @Description 路由选择功能
 * @Sonkwl Xiong
 * @Date 2022/03/09 10:18:59
 */
namespace procjson;

class apiapp{
    static $callbackarray=array();//callback数组

    //绑定GET方法的callback
    static public function Get($url,$callback){
        self::$callbackarray[$url]["GET"]=$callback;
    }
    //绑定POST方法的callback
    static public function Post($url,$callback){
        self::$callbackarray[$url]["POST"]=$callback;
    }
    //运行
    static public function Run(){
        $p="index";//默认?p=index
        if(isset($_GET["p"]) && !empty($_GET["p"])){
            $p=$_GET["p"];
        }
        if(!isset(self::$callbackarray[$p][$_SERVER["REQUEST_METHOD"]])){
            echo 'Not Define Function:['.$_SERVER["REQUEST_METHOD"].']'.$p;
            exit;
        }
        // self::$callbackarray[$p][$_SERVER["REQUEST_METHOD"]]();
        call_user_func(self::$callbackarray[$p][$_SERVER["REQUEST_METHOD"]]);
    }
    //模板处理
    static public function Templet($htmlpath,$res){
        if(!is_file($htmlpath)){
            echo $htmlpath.' 模板不存在';
            exit;
        }
        $html=file_get_contents($htmlpath);
        foreach ($res as $key => $value){
            $html=str_replace("{{".$key."}}",$value,$html);
        }
        echo $html;
    }
    //发布模式
    static public function Release(){
        ini_set('display_errors','Off');
    }
    //开启跨域
	static public function CanCross(){
		header('Access-Control-Allow-Origin:*');
		header("Access-Control-Allow-Credentials:true");
	}
}
?>